package com.fudan.backend.service.impl;

import com.fudan.backend.dao.CommodityDao;
import com.fudan.backend.dao.CommodityModifyRequestDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Commodity;
import com.fudan.backend.pojo.CommodityModifyRequest;
import com.fudan.backend.pojo.statics.RequestStatus;
import com.fudan.backend.service.CommodityModifyRequestService;
import com.fudan.backend.util.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommodityModifyRequestServiceImpl implements CommodityModifyRequestService {

    @Autowired
    CommodityModifyRequestDao commodityModifyRequestDao;

    @Autowired
    CommodityDao commodityDao;

    @Autowired
    Snowflake snowflake;

    @Override
    public RetCode addCommodityModifyRequest(CommodityModifyRequest commodityModifyRequest) {
        System.out.println(commodityModifyRequest);
        Date date = new Date();
        commodityModifyRequest.setCommodityModifyRequestId(((Long) snowflake.nextId()).toString());
        commodityModifyRequest.setInitiateTime(date);
        commodityModifyRequest.setProcessTime(null);
        commodityModifyRequest.setStatus(RequestStatus.APPLYING);

        int insertCommodityModifyRequest = commodityModifyRequestDao.insertCommodityModifyRequest(commodityModifyRequest);
        if(insertCommodityModifyRequest == 0){
            throw new BaseException(RetCode.COMMODITY_MODIFY_REQUEST_FAILURE);
        }
        return RetCode.COMMODITY_SUCCESS;
    }

    @Override
    @Transactional
    public RetCode processCommodityModifyRequest(CommodityModifyRequest commodityModifyRequest) {
        Date date = new Date();
        commodityModifyRequest.setProcessTime(date);
        int updateCommodityModifyRequest = commodityModifyRequestDao.updateCommodityModifyRequest(commodityModifyRequest);
        if(updateCommodityModifyRequest == 0){
            throw new BaseException(RetCode.COMMODITY_MODIFY_REQUEST_FAILURE);
        }

        String commodityId = commodityModifyRequest.getCommodityId();
        Integer status = commodityModifyRequest.getStatus();

        if(status == RequestStatus.APPROVED){
            Commodity commodity = commodityDao.selectCommodityByCommodityId(commodityId);
            commodity.setCommodityName(commodityModifyRequest.getCommodityName());
            commodity.setCategoryId(commodityModifyRequest.getCategoryId());
            commodity.setPrice(commodityModifyRequest.getPrice());
            commodity.setPictures(commodityModifyRequest.getPictures());
            commodity.setStocks(commodityModifyRequest.getStocks());
            commodity.setDescription(commodityModifyRequest.getDescription());
            commodityDao.updateCommodity(commodity);
        }
        return RetCode.COMMODITY_SUCCESS;
    }

    @Override
    public PageInfo<CommodityModifyRequest> getAllCommodityModifyRequest(int current, int pageSize) {
        PageHelper.startPage(current, pageSize);
        List<CommodityModifyRequest> commodityModifyRequests = commodityModifyRequestDao.selectAllCommodityModifyRequest();
        return new PageInfo<CommodityModifyRequest>(commodityModifyRequests, 5);
    }

    @Override
    public PageInfo<CommodityModifyRequest> getAllCommodityModifyRequestByStoreId(int current, int pageSize, String storeId) {
        PageHelper.startPage(current, pageSize);
        List<CommodityModifyRequest> commodityModifyRequests = commodityModifyRequestDao.selectAllCommodityModifyRequestByStoreId(storeId);
        return new PageInfo<CommodityModifyRequest>(commodityModifyRequests, 5);
    }
}
